package com.portal.gui.salesman;

import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import com.portal.logic.ConnectionManager;
import com.portal.logic.Customer;
import com.portal.logic.CustomerDAO;
import com.portal.logic.CustomerField;
import com.portal.logic.CustomerFieldDAO;
import com.portal.logic.CustomerSale;
import com.portal.logic.CustomerSaleDAO;
import com.portal.logic.Field;
import com.portal.logic.FieldDAO;
import com.portal.logic.Sale;
import com.portal.logic.SaleDAO;
import com.portal.logic.SerializationUtil;

//Mark Client and write comment if he didn't buy a product.



public class SalesmanPortal extends JFrame {
	
	private JPanel contentPane;
	
	public SalesmanPortal() {
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		setBounds(100, 100, 450, 314);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		getContentPane().setLayout(null);
		
		
		
		final FieldDAO fieldDAO = new FieldDAO(ConnectionManager.getConnection()) ;
		List<Field> fields = fieldDAO.selectAll();
		final JComboBox cmbFields = new JComboBox();
		Iterator<Field> iterator2 = fields.iterator();
		while (iterator2.hasNext()) {
			Field field = (Field) iterator2.next();
			cmbFields.addItem(field.getName());
		}
		cmbFields.setBounds(93, 141, 128, 22);
		getContentPane().add(cmbFields);
		
		
		final CustomerDAO customerDAO = new CustomerDAO(ConnectionManager.getConnection()) ;
		List<Customer> customers = customerDAO.selectAll();
		final JComboBox cmbCustomers = new JComboBox();
		Iterator<Customer> iterator = customers.iterator();
		while (iterator.hasNext()) {
			Customer customer = (Customer) iterator.next();
			cmbCustomers.addItem(customer.getName());
		}
		cmbCustomers.setBounds(132, 9, 271, 25);
		getContentPane().add(cmbCustomers);
		
		final SaleDAO salesDAO = new SaleDAO(ConnectionManager.getConnection()) ;
		List<Sale> sales = salesDAO.selectAll();
		final JComboBox cmbSales = new JComboBox();
		Iterator<Sale> iterator1 = sales.iterator();
		while (iterator1.hasNext()) {
			Sale sale = (Sale) iterator1.next();
			cmbSales.addItem(sale.getName());
		}
		cmbSales.setBounds(93, 61, 128, 22);
		getContentPane().add(cmbSales);
		
		JLabel lblClientsList = new JLabel("Select Customer");
		lblClientsList.setBounds(27, 13, 103, 16);
		getContentPane().add(lblClientsList);
		
		JButton btnAddSaleToCustomer = new JButton("add sale to Customer");
		btnAddSaleToCustomer.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String selectedCustomer = (String)cmbCustomers.getSelectedItem();
				String selectedSale = (String)cmbSales.getSelectedItem();
				
				CustomerDAO customerDAO = new CustomerDAO(ConnectionManager.getConnection());
				Customer customer = null;
				customer = customerDAO.selectByName(selectedCustomer);
				
				SaleDAO saleDAO = new SaleDAO(ConnectionManager.getConnection());
				Sale sale = null;
				sale = saleDAO.selectByName(selectedSale);
				
				CustomerSaleDAO customerSaleDAO = new CustomerSaleDAO(ConnectionManager.getConnection());
				CustomerSale customerSale = new CustomerSale();
				customerSale.setId(customerSaleDAO.selectMaxId()+1);
				customerSale.setCustomerId(customer.getId());
				customerSale.setSaleId(sale.getId());
				try {
					SerializationUtil.serialize(customerSale, "c:\\customer_sale.out");
				} catch (IOException ioe) {
					ioe.printStackTrace();
				}
				
				customerSaleDAO.insert();
				JOptionPane.showMessageDialog(null, "New Sale added to Customer!");
				
			}
		});
		btnAddSaleToCustomer.setBounds(233, 60, 170, 25);
		getContentPane().add(btnAddSaleToCustomer);
		
		JButton btnAddFieldToCustomer = new JButton("add field to Customer");
		btnAddFieldToCustomer.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String selectedCustomer = (String)cmbCustomers.getSelectedItem();
				String selectedField = (String)cmbFields.getSelectedItem();
				
				CustomerDAO customerDAO = new CustomerDAO(ConnectionManager.getConnection());
				Customer customer = null;
				customer = customerDAO.selectByName(selectedCustomer);
				
				FieldDAO saleDAO = new FieldDAO(ConnectionManager.getConnection());
				Field field = null;
				field = saleDAO.selectByName(selectedField);
				
				CustomerFieldDAO customerFieldDAO = new CustomerFieldDAO(ConnectionManager.getConnection());
				CustomerField customerField = new CustomerField();
				customerField.setId(customerFieldDAO.selectMaxId()+1);
				customerField.setCustomerId(customer.getId());
				customerField.setFieldId(field.getId());
				try {
					SerializationUtil.serialize(customerField, "c:\\customer_field.out");
				} catch (IOException ioe) {
					ioe.printStackTrace();
				}
				
				customerFieldDAO.insert();
				JOptionPane.showMessageDialog(null, "New Field added to Customer!");
			}
		});
		btnAddFieldToCustomer.setBounds(233, 140, 170, 25);
		getContentPane().add(btnAddFieldToCustomer);
		

		
		JLabel lblSalesList = new JLabel("Sales List");
		lblSalesList.setBounds(27, 64, 56, 16);
		getContentPane().add(lblSalesList);
		
	
		
		JLabel lblField = new JLabel("select field");
		lblField.setBounds(27, 145, 69, 16);
		getContentPane().add(lblField);
		
		JButton btnAddFeedbackTo = new JButton("Add FeedBack to Sale by Customer Scr");
		btnAddFeedbackTo.setBounds(132, 200, 275, 25);
		btnAddFeedbackTo.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				AddFeedbackToSaleByCustomerScr addFeedbackToSaleByCustomerScr = new AddFeedbackToSaleByCustomerScr();
				addFeedbackToSaleByCustomerScr.setVisible(true);
			}
		});
		getContentPane().add(btnAddFeedbackTo);
		
		final JButton btnBack = new JButton("Back");
		btnBack.setBounds(12, 239, 97, 25);
		btnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent actionevent) {
				Container frame = btnBack.getParent();
				do{
					frame = frame.getParent(); 
				}while (!(frame instanceof JFrame));                                      
				((JFrame) frame).dispose();
			}
		});
		getContentPane().add(btnBack);
		
		JButton btnCustomersPerSale = new JButton("Get Customers Per Sale");
		btnCustomersPerSale.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent actionevent) {
				//TODO
				String selectedSale = (String)cmbSales.getSelectedItem();
				GetCustomersPerSaleScr getCustomersPerSaleScr = new GetCustomersPerSaleScr(selectedSale);
				getCustomersPerSaleScr.setVisible(true);
			}
		});
		btnCustomersPerSale.setBounds(233, 90, 170, 25);
		getContentPane().add(btnCustomersPerSale);
	}
}
